package com.moju.controller;

import com.moju.common.Constant;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/file")
public class UploadController {

    @RequestMapping("/uploadFile")
    public void fileUpload(@RequestParam(required = false) MultipartFile pic,
                           HttpServletResponse response) throws IOException {

        String originalFilename = pic.getOriginalFilename();

        File file = new File(Constant.IMG_URL+originalFilename);

        if(!file.exists()){
            file.createNewFile();
        }

        pic.transferTo(file);

        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("img/"+originalFilename);

    }


}
